const Comment = require('../models/Comment')
const Article = require('../models/Article')

exports.addComment = async function (req, res, next) {
  let body = req.body
  const uid = req._id
  body.uid = uid
  console.log('body: ', body)
  try {
    const comment = await Comment.create(body)
    // 文章评论字段更新
    const articleId = body.aid
    const article = await Article.findById(articleId)
    article.comments.push(comment._id)
    await article.save()
    res.success('评论添加成功')
  } catch (err) {
    next(err)
  }
}

exports.deleteComment = async (req, res, next) => {
  const id = req.params.id
  try {
    await Comment.findByIdAndDelete(id)
    res.success('评论删除成功')
  } catch (err) {
    console.log('err: ', err)
  }
}
